//
//  MYY_LoginView.m
//  meiyinxiang
//
//  Created by 陈李飞 on 2017/10/26.
//  Copyright © 2017年 hbjApple. All rights reserved.
//

#import "MYY_LoginView.h"
#import "M80AttributedLabel.h"
#import "MYY_LoginTextField.h"
#import "Stalker.h"
#import "MYY_User.h"
#import "NJXTLoginHelper.h"
#import "MYYLoginCell.h"
#import "JXTColorHelper.h"
#import "XPauthorizationView.h"
#import "MYY_Me_Macro.h"
#import "MYY_LoginMarco.h"
#import "MYY_registerViewController.h"
#import "MYXForgotPasswordViewController.h"
#import "UIView+Additions.h"
#import "UMSocialQQHandler.h"
#import "UMSocial.h"
#define kLoginCellIdentifier @"loginCellIdentifier"

typedef void(^LoginBlock)(NSString *account, NSString *password);



@interface MYY_LoginView()<M80AttributedLabelDelegate, UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>
@property(nonatomic, strong) UIView *BGiconView;
@property(nonatomic, strong) UIImageView *iconImageView;
@property(nonatomic, strong) MYY_LoginTextField *userTextField;
@property(nonatomic, strong) MYY_LoginTextField *passwordTextField;
@property(nonatomic, strong) UIButton *loginButton;
@property(nonatomic, strong) M80AttributedLabel *bottomLabel;
@property(nonatomic, strong) M80AttributedLabel *registerLabel;
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UIView *tableBorderView;
@property(nonatomic, strong)XPauthorizationView *XPauthorizationV;

@property(nonatomic, strong) STStalker *stalker;
@property(nonatomic, assign) BOOL needAnimateAfterKeybordHide;

@property(nonatomic, copy) LoginBlock loginBlock;
@property(nonatomic, strong) NSMutableArray *users;

@property(nonatomic, assign) CGFloat tableHeight;

@property(nonatomic, strong) MYY_User *willDeleteUser;

@end


@implementation MYY_LoginView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        __weak typeof(self) weakSelf = self;
        
        UIImageView *view = [[UIImageView alloc] init];
        view.frame = self.bounds;
        if ( iphone6P) {
            view.image= [UIImage imageNamed:@"6P"];
        }else if (iphone6){
            view.image= [UIImage imageNamed:@ "beijing"];
        }
        else{
            view.image= [UIImage imageNamed:@"55s5c"];
        }
        
        view.userInteractionEnabled = YES;
        [self addSubview:view];
        
        [self addSubview:self.userTextField];
        [self addSubview:self.passwordTextField];
        [self addSubview:self.BGiconView];
        [self.BGiconView addSubview:self.iconImageView];
        [self addSubview:self.loginButton];
        [self addSubview:self.bottomLabel];
        [self addSubview:self.tableView];
        [self addSubview:self.tableBorderView];
        [self addSubview:self.XPauthorizationV];
        [self addSubview:self.registerLabel];
        
        self.needAnimateAfterKeybordHide = YES;
        self.XPauthorizationV.weixinImageViewB =^{
            NSLog(@"微信");
            
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
                UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToWechatSession];
                snsPlatform.loginClickHandler([weakSelf viewController],[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                        UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                        NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.data, response.error, response.message);
                        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                        NSString *kWeixins= [defaults objectForKey:kWeixin];
                        if (kWeixins.length) {
                           
                            
                        }
                        
                    }
                    
                });
                
            }else{
                [JXTHUDHelper showHUDText:@"您还没有安装微信" type:@"error" inView:nil];
            }
            
            
           
            
        };
        self.XPauthorizationV.QQmageViewB =^{
            NSLog(@"新浪");
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"weibosdk://"]]) {
                UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
                snsPlatform.haveWebViewAuth = YES;
                snsPlatform.needLogin = YES;
                snsPlatform.loginClickHandler([weakSelf viewController],[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                    //          获取微博用户名、uid、token等
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                        UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                        NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.message);
                        
                        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                        NSString *kWeixins= [defaults objectForKey:kSina];
                        if (kWeixins.length) {
                            
                            NSLog(@"----有新浪id");
                            
                        }else{
                            
                        }
                        
                    }
                    
                });
                
            }else{
                [JXTHUDHelper showHUDText:@"您还没有安装微博" type:@"error" inView:nil];
            }
            
        };
        self.XPauthorizationV.xinlangImageViewB =^{
            NSLog(@"qq");
            if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"mqq://"]]) {
                UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
                snsPlatform.loginClickHandler([weakSelf viewController],[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
                    //          获取微博用户名、uid、token等
                    if (response.responseCode == UMSResponseCodeSuccess) {
                        NSDictionary *dict = [UMSocialAccountManager socialAccountDictionary];
                        UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
                        
                        NSLog(@"\nusername = %@,\n usid = %@,\n token = %@ iconUrl = %@,\n unionId = %@,\n thirdPlatformUserProfile = %@,\n thirdPlatformResponse = %@ \n, message = %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL, snsAccount.unionId, response.data, response.error, response.message);
                        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                        NSString *kQQs= [defaults objectForKey:kQQ];
                        if (kQQs.length) {
                            
                           
                        }else{
                    
                        }
                        
                    }});
            }else{
                [JXTHUDHelper showHUDText:@"您还没有安装QQ" type:@"error" inView:nil];
            }
            
        };
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    WS(weakSelf)
    
    self.XPauthorizationV.frame = CGRectMake(0, HEIGHT-(HEIGHT)/4, WIDTH, (HEIGHT)/4);
    [self.userTextField setNormalIcon:@"haoma" highlightIcon:@"haoma" placeholder:@"账号" password:NO returnKeyType:UIReturnKeyNext dropDown:YES seperator:YES userSelectBlock:^{
//        weakSelf.userTextField.textField.keyboardType=UIKeyboardTypeNumberPad;;
        weakSelf.needAnimateAfterKeybordHide = NO;
        weakSelf.tableView.hidden = !weakSelf.tableView.hidden;
        weakSelf.tableBorderView.hidden = weakSelf.tableView.hidden;
        [weakSelf.userTextField updateSelectState:!weakSelf.tableView.hidden];
        [weakSelf.userTextField setNeedsLayout];
        [weakSelf.tableView reloadData];
        [weakSelf setNeedsLayout];
    } clearBlock:^{
        weakSelf.userTextField.textField.text = @"";
        weakSelf.passwordTextField.textField.text = @"";
        [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:nil];
    }nextBlock:^{
        [weakSelf.passwordTextField.textField becomeFirstResponder];
    } doneBlock:nil];
    [self.passwordTextField setNormalIcon:@"mima-" highlightIcon:@"mima-" placeholder:@"请输入密码" password:YES returnKeyType:UIReturnKeyJoin dropDown:NO seperator:NO userSelectBlock:nil clearBlock:^{
        weakSelf.passwordTextField.textField.text = @"";
        [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:nil];
        
    }nextBlock:nil doneBlock:^{
        [weakSelf login];
    }];
    
    // 开始时的状态
    [self beforeAnimations];
    
    [self.stalker when:UIKeyboardWillShowNotification then:^(NSNotification *notification) {
        NSDictionary *userInfo = notification.userInfo;
        CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        NSInteger curveOption = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
        if (duration > 0) {
            UIViewAnimationOptions option = (uint)(curveOption << 16);
            [UIView animateWithDuration:duration delay:0 options:option animations:^{
                [weakSelf afterAnimations];
            } completion:nil];
        }
        else {
            [weakSelf afterAnimations];
        }
    }];
    
    [self.stalker when:UIKeyboardWillHideNotification then:^(NSNotification *notification) {
        if (!weakSelf.needAnimateAfterKeybordHide) {
            weakSelf.needAnimateAfterKeybordHide = YES;
            return;
        }
        NSDictionary *userInfo = notification.userInfo;
        CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
        NSInteger curveOption = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
        if (duration > 0) {
            UIViewAnimationOptions option = (uint)(curveOption << 16);
            [UIView animateWithDuration:duration delay:0 options:option animations:^{
                [weakSelf beforeAnimations];
            } completion:nil];
        }
        else {
            [weakSelf beforeAnimations];
        }
    }];
    
    [self.stalker when:UITextFieldTextDidChangeNotification then:^(NSNotification *notification) {
        if (notification.object && ((UIView *)(notification.object)).tag == 10000) {
            // 自动填充
            NSArray *allUsers = [[NJXTLoginHelper helper] listAllUsers];
            __block NSString *willFilledPassword = @"";
            [allUsers enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                MYY_User *cUser = (MYY_User *)obj;
                if ([cUser.account isEqualToString:weakSelf.userTextField.textField.text]) {
                    willFilledPassword = cUser.password;
                    *stop = YES;
                }
            }];
            weakSelf.passwordTextField.textField.text = willFilledPassword;
        }
        
        
        if (![weakSelf.userTextField.textField.text isEqualToString:@""] && ![weakSelf.passwordTextField.textField.text isEqualToString:@""]) {
            [weakSelf updateEnableState:YES hasLogin:NO];
        }
        else {
            [weakSelf updateEnableState:NO hasLogin:NO];
        }
    }];
     [self.userTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
     self.userTextField.textField.keyboardType =UIKeyboardTypeNumberPad;
     [self.passwordTextField.textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
}


- (void)setUserList:(NSArray *)userList loginBlock:(void(^)(NSString *account, NSString *password))loginBlock
{
    self.users = [NSMutableArray arrayWithArray:userList];
    if (userList.count > 0) {
        MYY_User *defaultUser = [userList firstObject];
        self.userTextField.textField.text = defaultUser.account;
        self.passwordTextField.textField.text = defaultUser.password;
        self.loginButton.enabled = YES;
    }
    self.loginBlock = loginBlock;
    self.tableHeight = MIN(userList.count * 50, 50*3.5);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.tableView.frame = CGRectMake(0, CGRectGetMaxY(self.userTextField.frame), WIDTH, self.tableHeight);
    self.tableBorderView.frame = CGRectMake(0, CGRectGetMaxY(self.tableView.frame), WIDTH, 0.5);
   // XPauthorizationV
}

// 键盘弹出前的布局
- (void)beforeAnimations
{
    if (iphone5S || iphone4S) {
        self.BGiconView.frame = CGRectMake((WIDTH-105)/2.0, 65, 105, 105);
        self.userTextField.frame = CGRectMake(0, CGRectGetMaxY(self.BGiconView.frame)+39, WIDTH, 50);
    }
    else{
        self.BGiconView.frame = CGRectMake((WIDTH-129)/2.0, 89, 129, 129);
        self.userTextField.frame = CGRectMake(0, CGRectGetMaxY(self.BGiconView.frame)+57, WIDTH, 50);
        
       
    }
    self.iconImageView.frame = CGRectMake(10, 10, self.BGiconView.frame.size.width-20, self.BGiconView.frame.size.height-20);
     self.BGiconView.layer.cornerRadius =self.BGiconView.frame.size.width/2;
     self.iconImageView.layer.cornerRadius =(self.BGiconView.frame.size.width-20)/2;
    self.passwordTextField.frame = CGRectMake(0, CGRectGetMaxY(self.userTextField.frame), WIDTH-120, 50);
    self.loginButton.frame = CGRectMake(16, CGRectGetMaxY(self.passwordTextField.frame)+30, WIDTH-32, 40);
    
    self.bottomLabel.frame = CGRectMake(WIDTH-80-25, CGRectGetMaxY(self.userTextField.frame)+25, 80, 25);
    self.registerLabel.frame = CGRectMake(16, CGRectGetMaxY(self.loginButton.frame)+15, WIDTH-32, 40);
    [self setNeedsLayout];
}

// 键盘弹出后的布局
- (void)afterAnimations
{
    // self.userTextField.dropDownButton.selected = NO;
    self.tableView.hidden = YES;
    self.tableBorderView.hidden = self.tableView.hidden;
    if (iphone5S || iphone4S) {
        self.BGiconView.frame = CGRectMake((WIDTH-(105/2.0))/2.0, 46, 105/2.0, 105/2.0);
        self.userTextField.frame = CGRectMake(0, CGRectGetMaxY(self.BGiconView.frame)+28, WIDTH, 50);
    }
    else{
        self.BGiconView.frame = CGRectMake((WIDTH-(129/2.0))/2.0, 107, 129/2.0, 129/2.0);
        self.userTextField.frame = CGRectMake(0, CGRectGetMaxY(self.BGiconView.frame)+45, WIDTH, 50);
    }
     self.iconImageView.frame = CGRectMake(10, 10, self.BGiconView.frame.size.width-20, self.BGiconView.frame.size.height-20);
    self.BGiconView.layer.cornerRadius =self.BGiconView.frame.size.width/2;
    self.iconImageView.layer.cornerRadius = (self.BGiconView.frame.size.width-20)/2;
    self.passwordTextField.frame = CGRectMake(0, CGRectGetMaxY(self.userTextField.frame), WIDTH-120, 50);
    self.loginButton.frame = CGRectMake(16, CGRectGetMaxY(self.passwordTextField.frame)+30, WIDTH-32, 40);
    self.bottomLabel.frame = CGRectMake(WIDTH-80-25, CGRectGetMaxY(self.userTextField.frame)+25, 80, 25);
    self.registerLabel.frame = CGRectMake(16, CGRectGetMaxY(self.loginButton.frame)+15, WIDTH-32, 40);
    [self setNeedsLayout];
}

- (void)updateEnableState:(BOOL)enable hasLogin:(BOOL)hasLogin
{
    if (enable) {
        self.loginButton.enabled = YES;
        [self.loginButton setTitle:@"登录" forState:UIControlStateNormal];
    }
    else {
        self.loginButton.enabled = NO;
        [self.loginButton setTitle:hasLogin?@"登录中...":@"登录" forState:UIControlStateNormal];
    }
}

- (void)resignResponse
{
    [self.userTextField.textField resignFirstResponder];
    [self.passwordTextField.textField resignFirstResponder];
}

- (void)login
{
    if (![self checkPhoneTextField]) {
        [JXTHUDHelper showHUDText:@"请输入正确的手机号码" type:@"error" inView:self];
        return ;
    }
    if (![self checkPasswordTextField]) {
        [JXTHUDHelper showHUDText:@"请输入6至20位的密码" type:@"error" inView:self];
        return ;
    }
    self.loginBlock(self.userTextField.textField.text, self.passwordTextField.textField.text);
}

#pragma mark - touchMethod
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    self.needAnimateAfterKeybordHide = YES;
    self.tableView.hidden = YES;
    self.tableBorderView.hidden = self.tableView.hidden;
    [self.userTextField updateSelectState:!self.tableView.hidden];
    [self resignResponse];
}

#pragma mark - UITableView datasource and delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.users.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MYYLoginCell *cell = [tableView dequeueReusableCellWithIdentifier:kLoginCellIdentifier];
    MYY_User *user = self.users[indexPath.row];
    WS(weakSelf)
    [cell setUser:user deleteUserBlock:^(MYY_User *user) {
        // [[NJXTLoginHelper helper] removeUser:user.userId];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"删除用户" message:@"删除此账号将一并删除本地缓存哦，确定要删除吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"删除", nil];
        alertView.tag = [user.token longLongValue];
        weakSelf.willDeleteUser = user;
        [alertView show];
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MYY_User *user = self.users[indexPath.row];
    self.userTextField.textField.text = user.account;
    self.passwordTextField.textField.text = user.password;
    self.tableView.hidden = YES;
    self.tableBorderView.hidden = self.tableView.hidden;
    [self.userTextField updateSelectState:!self.tableView.hidden];
    self.loginButton.enabled = YES;
}

#pragma mark - M80Attribute delegate
- (void)m80AttributedLabel:(M80AttributedLabel *)label
             clickedOnLink:(id)linkData
{
    if ([linkData isEqualToString:@"register"]) {
        MYY_registerViewController *MYY_registerVC = [[MYY_registerViewController alloc] init];
        MYY_registerVC.registerBlock=^(NSString *user, NSString *password){
            self.userTextField.textField.text = user;
            self.passwordTextField.textField.text = password;
            self.loginButton.enabled = YES;
        };
        [[self viewController].navigationController pushViewController:MYY_registerVC animated:YES];
    }
    else if ([linkData isEqualToString:@"forgetPWD"]) {
        MYXForgotPasswordViewController *MYXForgotPasswordViewControl = [[MYXForgotPasswordViewController alloc] init];
        MYXForgotPasswordViewControl.navigationItem.title =@"忘记密码";
        MYXForgotPasswordViewControl.type=1;
        [[self viewController].navigationController pushViewController:MYXForgotPasswordViewControl animated:YES];
        
       
    }
}

#pragma mark - UIAlertView delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        if (self.willDeleteUser == nil) {
            return;
        }
        if ([self.userTextField.textField.text isEqualToString:self.willDeleteUser.account]) {
            self.userTextField.textField.text  =@"";
            self.passwordTextField.textField.text = @"";
            self.loginButton.enabled = NO;
        }
        [self.users removeObject:self.willDeleteUser];
        self.tableHeight = MIN(self.users.count * 50, 50*3.5);
        [self setNeedsLayout];
        [self.tableView reloadData];
        [[NJXTLoginHelper helper] removeUser:self.willDeleteUser.token];
        // 删除用户的缓存
        [[NJXTLoginHelper helper] clearCache:self.willDeleteUser];
    }
    else {
        self.willDeleteUser = nil;
    }
}


#pragma mark - property
- (UIImageView *)iconImageView
{
    if (_iconImageView == nil) {
        _iconImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loginicon"]];
        _iconImageView.clipsToBounds = YES;
//        _iconImageView.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
//        _iconImageView.layer.borderWidth = 1;//边框宽度
//        _iconImageView.backgroundColor=[UIColor redColor];
      // _iconImageView.image = [JXTColorHelper imageWithColor:[UIColor whiteColor]];
    }
    return _iconImageView;
}

- (UIView *)BGiconView
{
    if (_BGiconView == nil) {
        _BGiconView = [[UIView alloc] init];
        _BGiconView.clipsToBounds = YES;
        _BGiconView.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色fb7768
        _BGiconView.layer.borderWidth = 0;//边框宽度
//        _BGiconView.backgroundColor=MYYColorFromRGB(0xfb7768);
    }
    return _BGiconView;
}


- (MYY_LoginTextField *)userTextField
{
    if (_userTextField == nil) {
        _userTextField = [MYY_LoginTextField new];
        _userTextField.textField.tag = 10000;
       // _userTextField.backgroundColor =MYYColorFromRGB(0xbceae5);
    }
    return _userTextField;
}

- (MYY_LoginTextField *)passwordTextField
{
    if (_passwordTextField == nil) {
        _passwordTextField = [MYY_LoginTextField new];
        _passwordTextField.textField.tag = 10001;
       // _passwordTextField.backgroundColor =MYYColorFromRGB(0xbceae5);
    }
    return _passwordTextField;
}
- (XPauthorizationView *)XPauthorizationV
{
    if (_XPauthorizationV == nil) {
        _XPauthorizationV = [XPauthorizationView new];
       
    }
    return _XPauthorizationV;
}

- (UIButton *)loginButton
{
    if (_loginButton == nil) {
        _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginButton.layer setMasksToBounds:YES];
        [_loginButton.layer setCornerRadius:20.0];//MYYColorFromRGB(0xffffff)
        [_loginButton setBackgroundImage:[[JXTColorHelper imageWithColor:MYYheadColor] stretchableImageWithLeftCapWidth:1 topCapHeight:1] forState:UIControlStateNormal];
        [_loginButton setBackgroundImage:[[JXTColorHelper imageWithColor:MYYheadColor] stretchableImageWithLeftCapWidth:1 topCapHeight:1] forState:UIControlStateDisabled];
        [_loginButton setBackgroundImage:[[JXTColorHelper imageWithColor:MYYheadColor] stretchableImageWithLeftCapWidth:1 topCapHeight:1] forState:UIControlStateSelected];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateDisabled];
        _loginButton.layer.borderColor = [UIColor whiteColor].CGColor;//边框颜色
        _loginButton.layer.borderWidth = 1;//边框宽度

        [_loginButton setEnabled:NO];
        [_loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _loginButton;
}

- (M80AttributedLabel *)bottomLabel
{
    if (_bottomLabel == nil) {
        _bottomLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _bottomLabel.font = [UIFont systemFontOfSize:12];
        _bottomLabel.textColor = RGBACOLOR(215, 215, 215, 1.0);
        _bottomLabel.textAlignment = kCTTextAlignmentRight;
        _bottomLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        _bottomLabel.numberOfLines = 1;
        _bottomLabel.delegate = self;
        [_bottomLabel setText:@"忘记密码?"];
        [_bottomLabel addCustomLink:@"forgetPWD" forRange:NSMakeRange(0, 5) linkColor:[UIColor whiteColor]];
        //        [_bottomLabel addCustomLink:@"forgetPWD" forRange:NSMakeRange(7, 4) linkColor:[UIColor whiteColor]];
    }
    return _bottomLabel;
}

- (M80AttributedLabel *)registerLabel
{
    if (_registerLabel == nil) {
        _registerLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _registerLabel.font = [UIFont systemFontOfSize:12];
        _registerLabel.textColor = [UIColor whiteColor];
        _registerLabel.textAlignment = kCTTextAlignmentCenter;
        _registerLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:0];
        _registerLabel.numberOfLines = 1;
        _registerLabel.delegate = self;
        [_registerLabel setText:@"注册"];
        [_registerLabel addCustomLink:@"register" forRange:NSMakeRange(0, 4) linkColor:[UIColor clearColor]];
      
    }
    return _registerLabel;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.hidden = YES;
        [_tableView registerClass:[MYYLoginCell class] forCellReuseIdentifier:kLoginCellIdentifier];
    }
    return _tableView;
}

- (UIView *)tableBorderView
{
    if (_tableBorderView == nil) {
        _tableBorderView = [UIView new];
        //_tableBorderView.backgroundColor =MYYColorFromRGB(0x8fdcd4);
        _tableBorderView.hidden = YES;
    }
    return _tableBorderView;
}

- (STStalker *)stalker
{
    if (_stalker == nil) {
        _stalker = [STStalker new];
    }
    return _stalker;
}

/** 检查手机号格式 */
- (BOOL)checkPhoneTextField {
    return self.userTextField.textField.text.length == 11 ;
}

/** 检查密码格式 */
- (BOOL)checkPasswordTextField {
    NSString *passwordText =self.passwordTextField.textField.text;
    return passwordText.length >= 6 && passwordText.length <= 20;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
